package aar.test.tessocr.db.impl;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import aar.test.tessocr.db.DBManager;
import aar.test.tessocr.db.bean.DB_ClientInfo;
import aar.test.tessocr.db.gen.DB_ClientInfoDao;
import aar.test.tessocr.db.gen.DaoSession;
import aar.test.tessocr.utils.Utils;

/**
 * Created by sunshine on 2020/8/3
 */
public class ClientInfoDaoImp {
    private static ClientInfoDaoImp instance;
    private DB_ClientInfoDao dao;


    private ClientInfoDaoImp(Context context) {
        DaoSession daoSession = DBManager.getInstance(context).getDaoSession(context);
        dao = daoSession.getDB_ClientInfoDao();
    }

    public static synchronized ClientInfoDaoImp getInstance(Context context) {
        if (instance == null)
            instance = new ClientInfoDaoImp(context);
        return instance;
    }

    public static void init() {
        instance = null;
    }

    public synchronized void AddTelNum(String telNum) {
        QueryBuilder<DB_ClientInfo> queryBuilder = dao.queryBuilder();
        Query<DB_ClientInfo> query = queryBuilder.
                where(DB_ClientInfoDao.Properties.TelNum.eq(telNum)).build();

        List<DB_ClientInfo> beans = query.list();
        DB_ClientInfo bean = (beans == null || beans.size() == 0) ? null
                : beans.get(0);
        if (bean == null) {
            DB_ClientInfo bean1 = new DB_ClientInfo();
            bean1.setTelNum(telNum);
            dao.insert(bean1);
        }
    }

    public boolean hasOldTelNmu(String telNum) {
        ArrayList<DB_ClientInfo> db_clientInfos = (ArrayList<DB_ClientInfo>) dao.loadAll();
        if (null == db_clientInfos || db_clientInfos.size() == 0) {
            return false;
        }

        for (DB_ClientInfo db_clientInfo : db_clientInfos) {
            if (!Utils.isNull(db_clientInfo.getTelNum()) && db_clientInfo.getTelNum().equals(telNum)) {
                return true;
            }
        }

        return false;
    }

    public void deleteAll() {
        dao.deleteAll();
    }
}
